﻿Public Class frmEditUsageHistory
    Dim Strsql As String
    Public Primary_code As String
    Dim _mysql As New ClsSQLhelper
#Region "Function"
    Sub Load_Department()
        Try
            Strsql = "Select Department_ID,Department_Name from department"
            Dim DT_Budget As New DataTable
            DT_Budget = _mysql.GetMYSQLDataTable(Strsql, "department")
            With Me.CboDepartment
                .DataSource = DT_Budget
                .DisplayMember = "Department_Name"
                .ValueMember = "Department_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try

    End Sub
    Sub Load_Data()
        Try
            Strsql = "Select * from historyusage where historyusage_id = " & Primary_code
            Dim DT_History As New DataTable
            DT_History = _mysql.GetMYSQLDataTable(Strsql, "historyusage")
            Me.CboDepartment.SelectedValue = DT_History.Rows(0).Item("historyusage_department")
            Me.DtpDateStart.DateTime = DT_History.Rows(0).Item("historyusage_startdate")
            Me.MemoRemark.Text = DT_History.Rows(0).Item("historyusage_remarks").ToString

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    

#End Region
    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        Try
            Strsql = "Update historyusage set historyusage_department = " & Me.CboDepartment.SelectedValue & " , "
            Strsql = Strsql & " historyusage_startdate = '" & Pn_Framework.MysqlDateTimeFormat(DtpDateStart.DateTime, False) & "' , "
            Strsql = Strsql & " historyusage_remarks = '" & Me.MemoRemark.Text & "' where historyusage_id = " & Primary_code

            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "OK_Button_Click"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = Strsql
                    frmDebug.ShowDialog()
                    Exit Sub
                Case Else
                    MsgBox("ปรับปรุงข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการปรับปรุงข้อมูล")
            End Select
            DialogResult = Windows.Forms.DialogResult.OK
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Dispose()
    End Sub

    Private Sub frmEditUsageHistory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Department()
        Load_Data()
    End Sub

    Private Sub DtpDateStart_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DtpDateStart.EditValueChanged

    End Sub
End Class